home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n1.arc / TABEXP2.C < prev    next >
Text File  |  1990-06-08  |  1KB  |  54 lines

  1. /* Listing 2. A more structured tab expansion program */
  2.  
  3. #include <stdio.h>
  4.  
  5. /* Wrap up global variables for tab expansion into a struct */
  6.  
  7. typedef struct {
  8.   FILE *f;       /* input file stream              */
  9.   int col;       /* current column of current line */
  10.   int tabsize;   /* given tab size                 */
  11.   int tabcnt;    /* amount of spaces left in tab   */
  12. } tabexp;
  13.  
  14. void tabexp_init(tabexp *t, FILE *fp, int ts)
  15. /* A function to initialize tabexp structs */
  16. {
  17.    t->f = fp;
  18.    t->col = 0; t->tabsize = ts; t->tabcnt = 0;
  19. }
  20.  
  21. int tabexp_get(tabexp *t)
  22. /* Gets a character from the input file, */
  23. /* expanding tabs along the way          */
  24. {
  25.   int c;
  26.  
  27.   if (t->tabcnt) {  /* currently expanding tab */
  28.      t->tabcnt--; t->col++;
  29.      c = ' ';
  30.   }
  31.   else c = fgetc(t->f);
  32.  
  33.   if (c == 0x09) { /* tab handler */
  34.      t->tabcnt = t->tabsize - (t->col % t->tabsize);
  35.      return tabexp_get(t);
  36.   }
  37.   else {
  38.     if (c == '\n') t->col = 0; else t->col++;
  39.   }
  40.   return c;
  41. }
  42.  
  43. main()
  44. {
  45.   int c;
  46.   tabexp te;
  47.   tabexp_init(&te, stdin, 8);  /* use stdin, tab size of 8 */
  48.  
  49.   do { /* loop until no more characters in input */
  50.     c = tabexp_get(&te);
  51.     if (c == -1) break; else putc(c, stdout);
  52.   } while(1);
  53. }
  54.